package com.zx.idc.backend.gui.common.base;

import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.ds.common.entity.BaseEntity;
import com.zx.idc.ds.common.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

import static com.zx.idc.backend.gui.common.ResultBuilder.success;

/**
 * 基本的删除接口 API
 *
 * @author rxliuli
 */
@SuppressWarnings("unused")
public interface BaseDeleteApi<Service extends BaseService<Entity>, Entity extends BaseEntity> extends BaseDependencyInjectionApi<Service, Entity> {
    Logger log = LoggerFactory.getLogger(BaseDeleteApi.class);

    /**
     * 通用的删除方法
     *
     * @param id 实体主键
     * @return 删除是否成功，需要以 {@link Result#getData()} 作为判断标准
     */
    @PostMapping("/delete/{id}")
    default Result<Boolean> delete(@PathVariable("id") Long id) {
        return using(BaseDeleteApi.class, service -> success(service.deleteById(id)));
    }

    /**
     * 通用的批量删除方法
     *
     * @param idList 实体主键列表
     * @return 批量删除是否成功，需要以 {@link Result#getData()} 作为判断标准
     */
    @PostMapping("/delete/batch")
    default Result<Boolean> delete(@RequestBody List<Long> idList) {
        if (idList.isEmpty()) {
            return success(true);
        }
        return using(BaseDeleteApi.class, service -> success(service.deleteBatchIds(idList)));
    }
}
